package com.gandong8.hbase.web.controller;

import com.gandong8.hbase.tool.domain.HbaseQuery;
import com.gandong8.hbase.tool.service.HbaseServiceInter;
import com.gandong8.hbase.web.domain.OrderDO;
import com.gandong8.hbase.web.domain.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.*;

@Slf4j
@Controller
@RequestMapping(value = "/hbase",produces = "application/json;charset=UTF-8")
public class HbaseController {
    @Autowired
    HbaseServiceInter hbaseServiceInter;

    /**
     * 创建表
     * @return
     */
    @RequestMapping(value = "/table/create")
    @ResponseBody
    public String tableCreate() {
        hbaseServiceInter.createTable(OrderDO.class);
        return "ok";
    }

    /**
     * 删除表
     * @param tableName
     * @return
     */
    @RequestMapping(value = "/table/delete")
    @ResponseBody
    public String tableCreate(String tableName) {
        hbaseServiceInter.deleteTable(tableName);
        return "ok";
    }

    /**
     * 列出所有表
     * @return
     */
    @RequestMapping(value = "/table/list")
    @ResponseBody
    public Result tableList() {
        List<String> tableList = hbaseServiceInter.listTable();
        return Result.success(tableList);
    }

    /**
     * 显示表的列簇
     * @return
     */
    @RequestMapping(value = "/family/list")
    @ResponseBody
    public Result familyList() {
        List<String> tableList = hbaseServiceInter.listFamily(OrderDO.class);
        return Result.success(tableList);
    }

    /**
     * 添加记录
     * @return
     */
    @RequestMapping(value = "/data/add")
    @ResponseBody
    public Result dataAdd() {
        List<OrderDO> orderDOList = new ArrayList<>();
        OrderDO orderDO = new OrderDO();
        orderDO.setId(100L);
        orderDO.setCreateTime(new Date());
        orderDO.setPrice(BigDecimal.valueOf(12.05));
        orderDO.setItemId(123);
        orderDO.setItemName("橙子");
        orderDO.setUserId(123);
        orderDO.setAddress("中国");
        OrderDO orderDO1 = new OrderDO();
        orderDO1.setId(101L);
        orderDO1.setCreateTime(new Date());
        orderDO1.setPrice(BigDecimal.valueOf(3.05));
        orderDO1.setItemId(124);
        orderDO1.setItemName("苹果");
        orderDO1.setUserId(123);
        orderDO1.setAddress("中国");
        orderDO1.setMobile("12345678901");
        orderDOList.add(orderDO);
        orderDOList.add(orderDO1);
        Boolean result = hbaseServiceInter.save(orderDOList);
        return Result.run(result);
    }

    /**
     * 根据rowkey获取记录
     * @param key
     * @return
     */
    @RequestMapping(value = "/data/get")
    @ResponseBody
    public Result dataGet(String key) {
        if(key == null) {
            key = "100";
        }
        OrderDO orderDO = hbaseServiceInter.getRecord(OrderDO.class, key);
        return Result.success(orderDO);
    }

    /**
     * 根据rowkey删除记录
     * @param key
     * @return
     */
    @RequestMapping(value = "/data/del")
    @ResponseBody
    public Result dataDel(String key) {
        if(key == null) {
            key = "100";
        }
        hbaseServiceInter.delete(OrderDO.class, key);
        return Result.success();
    }

    /**
     *
     * @param keys
     * @return
     */
    @RequestMapping(value = "/data/list")
    @ResponseBody
    public Result dataList(String keys) {
        if(keys == null) {
            keys = "100,101";
        }
        List<String> keyList = Arrays.asList(keys.split(","));
        List<OrderDO> orderDOList = hbaseServiceInter.getRecords(OrderDO.class, keyList);
        return Result.success(orderDOList);
    }

    /**
     * 根据时间检索记录
     * @return
     */
    @RequestMapping(value = "/data/query")
    @ResponseBody
    public Result dataQuery() {
        HbaseQuery query = new HbaseQuery();
        query.setMaxStamp(System.currentTimeMillis());
        query.setMinStamp(System.currentTimeMillis()-3600000L);
        List<OrderDO> orderDOList = hbaseServiceInter.search(OrderDO.class, query);
        return Result.success(orderDOList);
    }

    /**
     * 根据列值检索记录
     * @return
     */
    @RequestMapping(value = "/data/query2")
    @ResponseBody
    public Result dataQuery2() {
        OrderDO orderDO = new OrderDO();
        orderDO.setUserId(123);
        HbaseQuery query = new HbaseQuery(orderDO);
        List<OrderDO> orderDOList = hbaseServiceInter.search(OrderDO.class, query);
        return Result.success(orderDOList);
    }

    /**
     * 获取某条记录的cell信息
     * @param key
     * @return
     */
    @RequestMapping(value = "/data/cells")
    @ResponseBody
    public Result dataCells(String key) {
        if(key == null) {
            key = "100";
        }
        return Result.success(hbaseServiceInter.getCells("order", key));
    }






}
